package com.guochu.pro.service.impl;

import com.guochu.pro.dao.AddressbookDao;
import com.guochu.pro.entity.Addressbook;
import com.guochu.pro.entity.Chat;
import com.guochu.pro.dao.ChatDao;
import com.guochu.pro.service.ChatService;
import com.guochu.utils.Pagination;
import com.guochu.common.infrastructure.IService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品分类表(Chat)表服务实现类
 *
 * @author makejava
 * @since 2021-07-05 23:05:08
 */
@Service
public class ChatServiceImpl extends IService implements ChatService {
    @Resource
    private ChatDao chatDao;
    @Resource
    private AddressbookDao addressbookDao;

    /**
     * 新增数据
     *
     * @param chat 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(Chat chat) {
        this.create(chat);
        return this.chatDao.insert(chat);
    }

    /**
     * 用户发送给客服
     *
     * @param chat
     * @return
     */
    @Override
    public int sendToAdmin(Chat chat) {
        this.create(chat);
        Map<String, Object> limit = new HashMap<>();
        limit.put("userId", chat.getFromUserId());
        List<Addressbook> addressbooks = this.addressbookDao.queryAllByLimit(limit);
        if (addressbooks.size() < 1) {
            Addressbook addressbook = new Addressbook();
            this.create(addressbook);
            addressbook.setUserId(chat.getFromUserId());
            addressbook.setUserName(chat.getFromUserName());
            addressbook.setHeadIcon(chat.getFromUserIcon());
            addressbook.setBestNewDate(LocalDateTime.now());
            addressbook.setNoReadNumber(1);
            this.addressbookDao.insert(addressbook);
        } else {
            Addressbook addressbook = addressbooks.get(0);
            addressbook.setUserName(chat.getFromUserName());
            addressbook.setHeadIcon(chat.getFromUserIcon());
            addressbook.setBestNewDate(LocalDateTime.now());
            addressbook.setNoReadNumber(addressbook.getNoReadNumber() + 1);
            this.addressbookDao.update(addressbook);
        }
        return this.chatDao.insert(chat);
    }


    /**
     * 修改数据
     *
     * @param chat 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Chat chat) {
        return this.chatDao.update(chat);
    }

    /**
     * 逻辑删除多条记录
     *
     * @param idList 主键
     * @return
     */
    @Override
    public int removeByIds(List<String> idList) {
        return this.chatDao.removeByIds(idList);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Chat queryById(String id) {
        return this.chatDao.queryById(id);
    }

    /**
     * 根据条件查询分页数据
     *
     * @param pagination 分页条件
     * @param limit      查询条件
     * @return 分页数据
     */
    @Override
    public List<Chat> queryPageByLimit(Pagination pagination, Map<String, Object> limit) {
        pagination.setTotal(this.chatDao.recordCount(limit));
        limit.put("offset", (pagination.getCurrentPage() - 1) * pagination.getPageSize());
        limit.put("pageSize", pagination.getPageSize());
        return this.chatDao.queryPageByLimit(limit);
    }

    /**
     * 用户小程序端获取聊天信息
     *
     * @param pagination
     * @param limit
     * @return
     */
    @Override
    public List<Chat> selectPageByObject(Pagination pagination, Map<String, Object> limit) {
        pagination.setTotal(this.chatDao.recordCount(limit));
        limit.put("offset", (pagination.getCurrentPage() - 1) * pagination.getPageSize());
        limit.put("pageSize", pagination.getPageSize());
        limit.put("userId", authService.getIdentity().getUser().getId());
        return this.chatDao.queryPageByLimit(limit);
    }

    /**
     * 客服获取聊天信息
     *
     * @param limit
     * @return
     */
    @Override
    public List<Chat> adminGetAll(Map<String, Object> limit) {
//        List<Addressbook> addressbooks = this.addressbookDao.queryAllByLimit(limit);
//        Addressbook addressbook = addressbooks.get(0);
//        addressbook.setNoReadNumber(0);
        // 清除通讯录未读数量
//        this.addressbookDao.update(addressbook);
//        // 清除未读记录
//        this.chatDao.cleanUnread(limit.get("aboutUser").toString());
        limit.put("userId", "admin");
        List<Chat> chats = this.chatDao.queryAllByLimit(limit);
        return chats;
    }


    /**
     * 根据条件查询全部数据
     *
     * @param limit 查询条件
     * @return 所有数据
     */
    @Override
    public List<Chat> queryAllByLimit(Map<String, Object> limit) {
        return this.chatDao.queryAllByLimit(limit);
    }


}
